package com.tc.service.impl;

import com.tc.domain.Sys_user;
import com.tc.domain.Sys_role;
import com.tc.domain.Sys_menu;
import com.tc.mapper.Sys_userMapper;
import com.tc.mapper.Sys_roleMapper;
import com.tc.mapper.Sys_menuMapper;
import com.tc.mapper.Sys_user_roleMapper;
import com.tc.service.ISys_userService;
import com.tc.vo.MenuVO;
import com.tc.vo.RoleVO;
import com.tc.vo.UserInfoVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户信息服务实现类
 * </p>
 *
 * @author tc
 * @since 2025-07-10
 */
@Service
public class Sys_userServiceImpl extends ServiceImpl<Sys_userMapper, Sys_user> implements ISys_userService {

    @Autowired
    private Sys_roleMapper roleMapper;

    @Autowired
    private Sys_menuMapper menuMapper;

    @Autowired
    private Sys_user_roleMapper userRoleMapper;

    @Override
    public Sys_user getByUsername(String username) {
        QueryWrapper<Sys_user> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        queryWrapper.eq("deleted", 0);
        return this.getOne(queryWrapper);
    }

    @Override
    public UserInfoVO getUserInfo(Long userId) {
        Sys_user user = this.getById(userId);
        if (user == null) {
            return null;
        }

        UserInfoVO userInfoVO = new UserInfoVO();
        BeanUtils.copyProperties(user, userInfoVO);
        
        // 查询用户角色
        List<RoleVO> roles = getUserRoles(userId);
        userInfoVO.setRoles(roles);

        return userInfoVO;
    }

    @Override
    public List<RoleVO> getUserRoles(Long userId) {
        List<Long> roleIds = userRoleMapper.selectRoleIdsByUserId(userId);
        if (roleIds.isEmpty()) {
            return new ArrayList<>();
        }

        List<Sys_role> roles = roleMapper.selectBatchIds(roleIds);
        return roles.stream().map(role -> {
            RoleVO roleVO = new RoleVO();
            BeanUtils.copyProperties(role, roleVO);
            return roleVO;
        }).collect(Collectors.toList());
    }

    @Override
    public List<MenuVO> getUserMenus(Long userId) {
        List<Sys_menu> menus = menuMapper.selectMenusByUserId(userId);
        return buildMenuTree(menus);
    }

    @Override
    public void updateLastLoginTime(Long userId) {
        Sys_user user = new Sys_user();
        user.setUserId(userId);
        user.setLastLoginTime(LocalDateTime.now());
        this.updateById(user);
    }

    /**
     * 构建菜单树
     */
    private List<MenuVO> buildMenuTree(List<Sys_menu> menus) {
        List<MenuVO> menuTree = new ArrayList<>();
        
        // 转换为VO
        List<MenuVO> menuVOs = menus.stream().map(menu -> {
            MenuVO menuVO = new MenuVO();
            BeanUtils.copyProperties(menu, menuVO);
            return menuVO;
        }).collect(Collectors.toList());

        // 构建树形结构
        for (MenuVO menu : menuVOs) {
            if (menu.getParentId() == 0) {
                // 根节点
                menu.setChildren(getChildren(menu.getMenuId(), menuVOs));
                menuTree.add(menu);
            }
        }

        return menuTree;
    }

    /**
     * 获取子菜单
     */
    private List<MenuVO> getChildren(Long parentId, List<MenuVO> allMenus) {
        List<MenuVO> children = new ArrayList<>();
        for (MenuVO menu : allMenus) {
            if (parentId.equals(menu.getParentId())) {
                menu.setChildren(getChildren(menu.getMenuId(), allMenus));
                children.add(menu);
            }
        }
        return children;
    }
}
